package com.itlong.cloud.utils.words;


import com.itlong.cloud.POJO.DTO.common.AccessToken;
import com.itlong.cloud.POJO.PO.wx.menu.*;
import com.itlong.cloud.enumerate.WeChatPushedType;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.node.JsonUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信菜单工具类
 *
 * @author Jiaqi.X
 * @version 1.0
 * @date 2016-12-22 14:15:43
 */
public class WechatMenuUtil {
    public static final String WECHAT_MENU_VIEW_TYPE="view";        //按钮类型 view
    public static final String WECHAT_MENU_CLICK_TYPE="click";      //按钮类型 click


    /**
     * 获取自定义菜单结构
     * @return menu
     */
    public static Menu getMenu(){
        Menu menu=new Menu();
        List<ComplexButton> complexs=new ArrayList<ComplexButton>();        //一级菜单集合
        /**
         * 目前菜单结构
         * 查找商铺：无二级菜单
         * 咱家服务：问卷调查，金融服务，投放铺源，委托找铺,商家反馈
         * 咱家铺子：咨询顾问，商务合作，APP下载，开店宝典
         */
        //1.第一级菜单
        ComplexButton complexButton=new ComplexButton();
        complexButton.setName("停车缴费");
        complexButton.setType(WechatMenuUtil.WECHAT_MENU_CLICK_TYPE);
        List<Button> buttons=new ArrayList<>();
        //临时车缴费
        ViewButton viewButton1_1=new ViewButton("临时车辆缴费",WechatMenuUtil.WECHAT_MENU_VIEW_TYPE,"http://develop.wx.jia-r.com/temporaryCarIndex.html");
        buttons.add(viewButton1_1);
        //业主会员
        ViewButton viewButton1_2=new ViewButton("月卡车辆缴费",WechatMenuUtil.WECHAT_MENU_VIEW_TYPE,"http://develop.wx.jia-r.com/monthCardIndex.html");
        buttons.add(viewButton1_2);
        complexButton.setSub_button(buttons);
        complexs.add(complexButton);
        //1.第一级菜单
        ComplexButton complexButton2=new ComplexButton();
        complexButton2.setName("访客预约");
        complexButton2.setType(WechatMenuUtil.WECHAT_MENU_CLICK_TYPE);
        List<Button> buttons2=new ArrayList<>();
        //临时车缴费
        ViewButton viewButton2_1=new ViewButton("我是访客",WechatMenuUtil.WECHAT_MENU_VIEW_TYPE,"http://develop.wx.jia-r.com/temporaryCarIndex.html");
        buttons2.add(viewButton2_1);
        //业主会员
        ViewButton viewButton2_2=new ViewButton("我是员工",WechatMenuUtil.WECHAT_MENU_VIEW_TYPE,"http://develop.wx.jia-r.com/monthCardIndex.html");
        buttons2.add(viewButton2_2);
        complexButton2.setSub_button(buttons2);
        complexs.add(complexButton2);
        //填充菜单
        menu.setButton(complexs);
        return menu;
    }


    /**
     * 获取默认菜单
     * @param accessToken accessToken
     * @return
     */
    public static String getMenu(String accessToken){
        // 访问微信服务器
        String url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info";
        String message="";
        try {
            Map<String,String> params=new HashMap<String, String>();
            params.put("access_token",accessToken);
            message = HttpProtocolUtil.httpGet(url, params);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return message;
    }



    /**
     * 创建自定义菜单
     * @param menu 菜单json串
     * @return
     */
    public static boolean createMenu(String menu,String accessToken){
        Map<String,Object> resultMap=null;
        String path="https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+accessToken;
        try {
            URL url=new URL(path);
            HttpURLConnection http = (HttpURLConnection)url.openConnection();
            http.setDoOutput(true);
            http.setDoInput(true);
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            http.connect();
            OutputStream os = http.getOutputStream();
            os.write(menu.getBytes("UTF-8"));
            os.close();
            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] bt = new byte[size];
            is.read(bt);
            String result=new String(bt,"UTF-8");
            resultMap= JsonUtil.toObject(result, Map.class);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultMap.get("errmsg").equals("ok");
    }
}
